import { createRouter, createWebHashHistory } from "vue-router";
import useUserStore from "@/stores/modules/user";
import { showToast } from "vant";

// 定义需要登录才能访问的路由
const authRoutes = ['/order', '/favor', '/message', '/profile/info', '/profile/security', '/profile/payment']

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            redirect: "/home",
        },
        {
            path: "/home",
            component: () => import("@/views/home/home.vue")
        },
        {
            path: "/favor",
            component: () => import("@/views/favor/favor.vue"),
            meta: {
                requiresAuth: true
            }
        },
        {
            path: "/order",
            component: () => import("@/views/order/order.vue"),
            meta: {
                requiresAuth: true
            }
        },
        {
            path: "/message",
            component: () => import("@/views/message/message.vue"),
            meta: {
                requiresAuth: true
            }
        },
        {
            path: "/city",
            component: () => import("@/views/city/city.vue"),
            meta: {
                showTabBar: true
            }
        },
        {
            path: "/search",
            component: () => import("@/views/search/search.vue"),
            meta: {
                showTabBar: true
            }
        },
        {
            path: "/detail/:id",
            component: () => import("@/views/detail/detail.vue"),
        },
        // 个人中心相关路由
        {
            path: "/profile",
            component: () => import("@/views/profile/profile.vue"),
            meta: {
                requiresAuth: false
            }
        },
        {
            path: "/profile/info",
            component: () => import("@/views/profile/cpns/profile-info.vue"),
            meta: {
                requiresAuth: true
            }
        },
        {
            path: "/profile/security",
            component: () => import("@/views/profile/cpns/profile-security.vue"),
            meta: {
                requiresAuth: true
            }
        },
        {
            path: "/profile/payment",
            component: () => import("@/views/profile/cpns/profile-payment.vue"),
            meta: {
                requiresAuth: true
            }
        },
        // 登录注册路由
        {
            path: "/login",
            component: () => import("@/views/login/login.vue"),
            meta: {
                hideTabBar: true
            }
        },
        {
            path: "/register",
            component: () => import("@/views/register/register.vue"),
            meta: {
                hideTabBar: true
            }
        },
    ],
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
    const userStore = useUserStore()

    // 判断页面是否需要登录权限
    if (to.meta.requiresAuth) {
        // 如果用户已登录，允许访问
        if (userStore.isLoggedIn()) {
            next()
        } else {
            // 未登录，提示用户并重定向到登录页
            showToast({
                type: 'fail',
                message: '请先登录',
                duration: 1500
            })
            // 将用户想要访问的页面路径作为查询参数，登录成功后跳回该页面
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        }
    } else {
        // 不需要登录权限的页面直接放行
        next()
    }
})

export default router